<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_kernelDensityJobService"></title>
    <script type="text/javascript" include="jquery,bootstrap,widgets" src="../js/include-web.js"></script>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="width: 100%;height:100%"></div>
    <script type="text/javascript" src="../../dist/leaflet/include-leaflet.js"></script>
    <script type="text/javascript">
        var host = window.isLocal ? window.server : "https://iserver.supermap.io";
        var layer, processingUrl = host + "/iserver/services/distributedanalyst/rest/v1/jobs",
            mapURL = host + "/iserver/services/map-world/rest/maps/World",
            map = L.map('map', {
                crs: L.CRS.EPSG4326,
                center: [40.75, -73.95],
                maxZoom: 18,
                zoom: 12
            }),
            info = L.control({
                position: 'topright'
            });
        new L.supermap.TiledMapLayer(mapURL).addTo(map);

        info.onAdd = function () {
            var popup = L.DomUtil.create('div');
            popup.style.width = '300px';
            popup.innerHTML = "<div class='panel panel-default'>" +
                "<div class='panel-heading'>" +
                "<h3 class='panel-title'>" + resources.text_kernelDensityJobService + "</h3>" +
                "</div>" +
                "<div class='panel-body'>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_inputData + "<span title='" + resources.text_requiredField +
                "' style='color: red;'> * </span>　</span>" +
                "<input id='datasetName' type='text' class='form-control' value='samples_newyork_taxi_2013-01_14k'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_analysisMethod + "<span title='" + resources.text_requiredField +
                "' style='color: red;'> * </span>　</span>" +
                "<div>" +
                "<select class='form-control' id='method' name='clientType'>" +
                "<option value='0' selected='selected'>" + resources.text_simpleDenAnalyst + "</option>" +
                "<option value='1'>" + resources.text_input_value_densityAnalyst + "</option>" +
                "</select></div>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_meshSurfaceType + "<span title='" + resources.text_requiredField +
                "' style='color: red;'> * </span></span>" +
                "<select class='form-control' id='meshType' name='clientType'>" +
                "<option value='0' selected='selected'>" + resources.text_4grid + "</option>" +
                "<option value='1'>" + resources.text_6grid + "</option>" +
                "</select>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_weightField + "</span>" +
                "<input id='fields' type='text' class='form-control' value='col7,col8'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_analysisRange + "　　</span>" +
                "<input id='query' type='text' class='form-control' value='-74.150, 40.550, -73.750, 40.950'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_gridSize + "<span title='" + resources.text_requiredField +
                "' style='color: red;'> * </span>　</span>" +
                "<input id='resolution' type='text' class='form-control' value='80'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_gridSizeUnit + "</span>" +
                "<select class='form-control' id='meshSizeUnit' name='clientType'>" +
                "<option value='Meter' selected='selected'>Meter</option>" +
                "<option value='Kilometer'>Kilometer</option>" +
                "<option value='Yard'>Yard</option>" +
                "<option value='Foot'>Foot</option>" +
                "<option value='Mile'>Mile</option></select></div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_searchRadius + "<span title='" + resources.text_requiredField +
                "' style='color: red;'> * </span>　</span>" +
                "<input id='radius' type='text' class='form-control' value='300'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_searchRadiusUnit + "</span>" +
                "<select class='form-control' id='radiusUnit' name='clientType'>" +
                "<option value='Meter' selected='selected'>Meter</option>" +
                "<option value='Kilometer'>Kilometer</option>" +
                "<option value='Yard'>Yard</option>" +
                "<option value='Foot'>Foot</option>" +
                "<option value='Mile'>Mile</option></select></div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_areaUnit + "</span>" +
                "<select class='form-control' id='areaUnit' name='clientType'>" +
                "<option value='SquareMile' selected='selected'>SquareMile</option>" +
                "<option value='Hectare'>Hectare</option>" +
                "<option value='Are'>Are</option>" +
                "<option value='Acre'>Acre</option>" +
                "<option value='SquareFoot'>SquareFoot</option>" +
                "<option value='SquareYard'>SquareYard</option>" +
                "<option value='SquareMeter'>SquareMeter</option></select></div><p>" +
                "<div align='right'>" +
                "<input type='button' id='btn' class='btn btn-primary' value='" + resources.text_analyst + "'/>" +
                "</div></div></div>";
            handleMapEvent(popup, this._map);
            return popup;
        };
        info.addTo(map);

        var processingService = new L.supermap.ProcessingService(processingUrl, {
            withCredentials: window.isLocal
        });
        L.supermap.SecurityManager.registerToken(processingUrl, window.exampleToken);


        function getQuery() {
            if ($('#query').val() === "") {
                return "";
            }
            var query = [];
            $('#query').val().split(',').map(function (el) {
                query.push(parseFloat(el));
            });
            return L.latLngBounds(L.latLng(query[1], query[0]), L.latLng(query[3], query[2]));
        }

        $('#btn').on('click', function () {
            if ($('#msg_container')[0]) {
                $('#msg_container').remove();
            }
            widgets.loader.showLoader();
            if (map && layer) {
                map.removeLayer(layer);
            }
            var kernelDensityJobParameter = new L.supermap.KernelDensityJobParameter({
                datasetName: $('#datasetName').val(),
                resolution: $('#resolution').val(),
                method: $('#method option:selected').attr('value'),
                meshType: $('#meshType option:selected').attr('value'),
                fields: $('#fields').val(),
                query: getQuery(),
                radius: $('#radius').val(),
                meshSizeUnit: $('#meshSizeUnit option:selected').attr('value'),
                radiusUnit: $('#radiusUnit option:selected').attr('value'),
                areaUnit: $('#areaUnit option:selected').attr('value'),
            });
            processingService.addKernelDensityJob(kernelDensityJobParameter, function (serviceResult) {
                if (serviceResult.error) {
                    widgets.loader.removeLoader();
                    var errorMsg = serviceResult.error.errorMsg || "code: " + serviceResult.error.code;
                    widgets.alert.showAlert(resources.msg_createFailed + "<br>" + errorMsg, false);
                    return;
                }
                serviceResult.result.setting.serviceInfo.targetServiceInfos.map(function (info) {
                    if (info.serviceType === 'RESTMAP') {
                        L.supermap.FetchRequest.get(info.serviceAddress + '/maps').then(function (
                            response) {
                            return response.json();
                        }).then(function (result) {
                            var mapUrl = result[0].path;
                            layer = new L.supermap.TiledMapLayer(mapUrl, {
                                noWrap: true,
                                transparent: true
                            });
                            layer.addTo(map);
                            widgets.loader.removeLoader();
                        });
                    }
                });
            });
        });

        function handleMapEvent(div, map) {
            if (!div || !map) {
                return;
            }
            div.addEventListener('mouseover', function () {
                map.dragging.disable();
                map.scrollWheelZoom.disable();
                map.doubleClickZoom.disable();
            });
            div.addEventListener('mouseout', function () {
                map.dragging.enable();
                map.scrollWheelZoom.enable();
                map.doubleClickZoom.enable();
            });
            $("#model").on('shown.bs.modal', function () {
                map.dragging.disable();
                map.scrollWheelZoom.disable();
                map.doubleClickZoom.disable();
            });
            $("#model").on('hidden.bs.modal', function () {
                map.dragging.enable();
                map.scrollWheelZoom.enable();
                map.doubleClickZoom.enable();
            })
        }
    </script>
</body>

</html>